package com.gitee.base.modules.core.controller;

import com.gitee.base.modules.core.service.ISysUserLoginLogService;
import com.gitee.base.security.permission.annotation.RequiresPermissionsDesc;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/core/stat")
public class CoreStatController {
    private final static ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private ISysUserLoginLogService sysUserLoginLogService;

    @RequiresPermissionsDesc(folder = "运行概览",menu = "用户统计",button = "查看",page = true)
    @RequiresPermissions("core:stat:list")
    @GetMapping("/loginlog")
    public String loginlog(ModelMap modelMap) {
        Map<String,Long> dataMap = new HashMap<>();

        //-------------登录统计------------------
        List<String> dateList = new ArrayList<>();
        List<Long> countList = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        for(int i=30;i>=0;i--){
            dateList.add(localDate.minusDays(i).toString());
        }
        List<Map<String,Object>> loginLogList = this.sysUserLoginLogService.countUserLoginLogByDateList(dateList);
        if(loginLogList!=null && !loginLogList.isEmpty()){
            for(Map<String,Object> map:loginLogList){
                dataMap.put(map.get("date").toString(),Long.valueOf(map.get("count").toString()));
            }
        }

        for(String date:dateList){
            Long count = dataMap.get(date);
            if(count==null){
                countList.add(0L);
            }else{
                countList.add(count);
            }
        }
        modelMap.put("dateList",dateList);
        modelMap.put("countList",countList);
        //-------------登录统计------------------

        return "core/stat/loginlog";
    }

    @RequiresPermissionsDesc(folder = "运行概览",menu = "设备统计",button = "查看",page = true)
    @RequiresPermissions("core:stat:list")
    @GetMapping("/device")
    public String device(ModelMap modelMap) throws JsonProcessingException {
        //-------------登录用户操作系统统计-----------------
        List<Map<String,Object>> loginOsList = this.sysUserLoginLogService.countUserLoginOsByDateRange(LocalDateTime.now().minusDays(30),LocalDateTime.now());
        if(loginOsList!=null && !loginOsList.isEmpty()){
            modelMap.put("deviceOs",objectMapper.writeValueAsString(loginOsList));
        }
        //-------------登录用户操作系统统计-----------------

        //-------------登录用户浏览器统计-----------------
        List<Map<String,Object>> loginBrowerList = this.sysUserLoginLogService.countUserLoginBrowerByDateRange(LocalDateTime.now().minusDays(30),LocalDateTime.now());
        if(loginOsList!=null && !loginOsList.isEmpty()){
            modelMap.put("deviceBrower",objectMapper.writeValueAsString(loginBrowerList));
        }
        //-------------登录用户操作系统统计-----------------

        return "core/stat/device";
    }

}
